#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void SL_init(SL* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->sz = 0;
}

void check_capacity(SL*ps)
{
	assert(ps);
	if (ps->sz==ps->capacity)
	{
		int new_capacity = ps->capacity == 0? 4 : ps->capacity * 2;
		SLDatatype* ptr = (SLDatatype*)realloc(ps->a, sizeof(SLDatatype)* new_capacity);
		if (ptr != NULL)
		{
			ps->a = ptr;
			ptr = NULL;
			ps->capacity = new_capacity;
		}
		else
		{
			perror("realloc");
			exit(-1);
		}
	}
}

void SLPushFront(SL*ps, SLDatatype x)
{
	assert(ps);
	check_capacity(ps);
	int i = 0;
	for ( i = ps->sz-1; i > -1; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->sz++;
}

void SLPopFront(SL*ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i <ps->sz; i++)
	{
		ps->a[i] = ps->a[i+1];
	}
	ps->sz--;
}

void SLPushBack(SL*ps,SLDatatype x)
{	
	assert(ps);
	check_capacity(ps);
	ps->a[ps->sz] = x;
	ps->sz++;
}
void SLPopback(SL*ps)
{
	assert(ps);
	ps->sz--;
}
void SLinsert(SL*ps, int pos, SLDatatype x)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos <= ps->sz);
	check_capacity(ps);
	int i = 0;
	for ( i = ps->sz-1; i >= pos; i--)
	{
		ps->a[i+1] = ps->a[i];
	}
	ps->a[pos] = x;
	ps->sz++;
}

void SLerase(SL*ps, int pos)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos <= ps->sz);
	int i = 0;
	for ( i = pos; i <ps->sz ; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->sz--;
}

void SLshow(SL*ps)
{
	assert(ps);
	int i = 0;
	for ( i = 0; i < ps->sz; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SLdestroy(SL*ps)
{
	assert(ps);
	ps->sz = 0;
	ps->capacity = 0;
	free(ps->a);
	ps->a = NULL;
}

void SLFind(SL*ps, SLDatatype x)
{
	int i = 0;
	for ( i = 0; i < ps->sz; i++)
	{
		if (ps->a[i]==x)
		{
			return i;
		}
	}
	return 0;
}